package com.hyfrogx.modules.zoom.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyfrogx.common.utils.QueryExt;
import com.hyfrogx.common.utils.R;
import com.hyfrogx.modules.app.entity.User;
import com.hyfrogx.modules.app.service.UserService;
import com.hyfrogx.modules.app.utils.FrogConstant;
import com.hyfrogx.modules.sys.controller.AbstractController;
import com.hyfrogx.modules.zoom.entity.HyUserZoomOrder;
import com.hyfrogx.modules.zoom.service.HyUserZoomOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * @author ：Li9527
 * @date ：Created in 2019-04-08 11:07
 * @description：账号订单控制类
 * @modified By：
 * @version: 1.0
 */
@RestController
@RequestMapping("/zoom/order")
@Api(value = "账号订单controller", tags = "后台--账号订单管理")
public class ZoomOrderController extends AbstractController {

    @Autowired
    HyUserZoomOrderService hyUserZoomOrderService;

    @Autowired
    UserService frogUserService;

    @PostMapping("create")
    @ApiOperation("创建账号订单")
    public R create(@RequestBody @Valid HyUserZoomOrder hyUserZoomOrder, BindingResult result) {
        if (result.hasErrors()) {
            return R.error(result.getAllErrors().get(0).getDefaultMessage());
        }
        // 获取uid
        User frogUserEntity = frogUserService.getOne(new QueryWrapper<User>().eq("phone", hyUserZoomOrder.getPhone()));
        if (frogUserEntity == null) {
            return R.error("你输入的手机号不是系统用户，请核实输入是否有误");
        }
        hyUserZoomOrder.setCreateTime(new Date());
        hyUserZoomOrder.setUid(frogUserEntity.getUid());
        hyUserZoomOrder.setStatus(FrogConstant.BasicStatus.TRUE.getValue());
        hyUserZoomOrder.setOrderNo(getOutNo());
        Boolean flag = hyUserZoomOrderService.save(hyUserZoomOrder);
        if (flag) {
            return R.ok();
        } else {
            return R.error("新增失败");
        }
    }

    @GetMapping("list")
    @ApiOperation("获取账号订单列表")
    public R list(@RequestParam Map<String, Object> params) {
        params.put("orderByField", "create_time");
        params.put("isAsc", false);
        params.put("status", FrogConstant.BasicStatus.TRUE.getValue());
        Page values = hyUserZoomOrderService.selectPageByConditions(new QueryExt(params));
        return R.ok().put("page", values);
    }

    @PostMapping("edit")
    @ApiOperation("编辑账号订单信息")
    public R edit(@RequestBody HyUserZoomOrder hyUserZoomOrder) {
        if (hyUserZoomOrder.getId() == null) {
            return R.error("主键id必传");
        }
        HyUserZoomOrder order = hyUserZoomOrderService.getById(hyUserZoomOrder.getId());
        if (order == null) {
            return R.error("对象不存在");
        }
        // 如果手机号有改动
        if (!hyUserZoomOrder.getPhone().equals(order.getPhone())) {
            // 获取uid
            User frogUserEntity = frogUserService.getOne(new QueryWrapper<User>().eq("phone", hyUserZoomOrder.getPhone()));
            if (frogUserEntity == null) {
                return R.error("手机号不存在平台,请知悉");
            }
            hyUserZoomOrder.setUid(frogUserEntity.getUid());
        }
        hyUserZoomOrder.setUpdateTime(new Date());
        Boolean flag = hyUserZoomOrderService.updateById(hyUserZoomOrder);
        if (flag) {
            return R.ok();
        } else {
            return R.error("修改失败");
        }
    }

    /**
     * 删除账号订单信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "delete/{id}")
    @ApiOperation("删除账号订单信息")
    public R delete(@PathVariable int id) {
        try {
            if (id == 0) {
                return R.error("id不合法");
            }
            HyUserZoomOrder order = hyUserZoomOrderService.getById(id);
            if (order == null) {
                return R.error("订单为空");
            }
            order.setStatus(FrogConstant.BasicStatus.FLASE.getValue());
            order.setUpdateTime(new Date());
            hyUserZoomOrderService.updateById(order);
            return R.ok();
        } catch (Exception e) {
            return R.error(e.getMessage());
        }
    }

    public static String getOutNo() {
        Calendar now = Calendar.getInstance();
        int year = now.get(Calendar.YEAR);
        int month = now.get(Calendar.MONTH) + 1;
        String monthN;
        if (month < 10) {
            monthN = "0" + month + "";
        } else {
            monthN = month + "";
        }
        int day = now.get(Calendar.DAY_OF_MONTH);
        String outNo = year + "" + monthN + day + "" + System.currentTimeMillis() + "";
        return outNo;
    }


}
